package com.kpx.app.view.webview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kpx.app.base.ActivityBase;
import com.kpx.app.view.MyWebView;
import com.kpx.app.wigdet.DialogWithdrawInputPwd;


public class MyWebViewClient extends WebViewClient {
    private final WebView myWebView;
    private final Context context;
    private View fragmentRootView = null;
    private Handler handler;
    private int[] currentLocation;
    private View loadFailView;//网页加载失败时的提示
    private DialogWithdrawInputPwd dialogWithdrawInputPwd;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }


    public MyWebViewClient(Context context, WebView myWebView) {
        this.context = context;
        this.myWebView = myWebView;
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("tel:")) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DIAL);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
            return true;
        }
        return super.shouldOverrideUrlLoading(view, url);
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        addJSListner();
        if (view instanceof MyWebView) {
            MyWebView myWebView = (MyWebView) view;
            myWebView.setCanShowNext(true);
            myWebView.setCanShowPrevious(true);
        }

        super.onPageFinished(view, url);
        showLoadingDialog(false);
        view.scrollBy(0, -100);
        view.scrollBy(0, 100);
        if (!view.getSettings().getLoadsImagesAutomatically()) {
            view.getSettings().setLoadsImagesAutomatically(true);
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        ((Activity) context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        showLoadingDialog(true);
        super.onPageStarted(view, url, favicon);
    }

    /**
     * 设置加载框的显示
     *
     * @param b true显示，false不显示
     */
    private void showLoadingDialog(boolean b) {
        if (context instanceof ActivityBase) {
            if (b) {
                ((ActivityBase) context).showLoadingDialog();
            } else {
                ((ActivityBase) context).dissmissLoadingDialog();
            }
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        if (loadFailView != null && loadFailView.getVisibility() == View.GONE) {
            loadFailView.setVisibility(View.VISIBLE);
        }
        if (context instanceof ActivityBase) {
            ((ActivityBase) context).dissmissLoadingDialog();
        }
    }

    @android.webkit.JavascriptInterface
    private void addJSListner() {
    }

    /**
     * 设置网页加载失败时的提示
     *
     * @param loadFailView
     */
    public void setLoadFailView(View loadFailView) {
        this.loadFailView = loadFailView;
    }
}